package com.pkk.spring.cloud.service.gateway.filter.gateway;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter;
import org.springframework.http.HttpStatus;

/**
 * 请求limit限制
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-29 13:45
 **/
public class RequestCustomRateLimiterGatewayFilterFactory extends
    AbstractGatewayFilterFactory<RequestCustomRateLimiterGatewayFilterFactory.Config> {


  /**
   * 当前bean的名称
   */
  public static final String RequestCustomRateLimiterGatewayFilterFactory_BEANNAME = "requestCustomRateLimiterGatewayFilterFactory";


  private final RateLimiter defaultRateLimiter;
  private final KeyResolver defaultKeyResolver;


  public RequestCustomRateLimiterGatewayFilterFactory(RateLimiter defaultRateLimiter, KeyResolver defaultKeyResolver) {
    //必须要这一步
    super(RequestCustomRateLimiterGatewayFilterFactory.Config.class);
    this.defaultRateLimiter = defaultRateLimiter;
    this.defaultKeyResolver = defaultKeyResolver;
  }

  public RateLimiter getDefaultRateLimiter() {
    return defaultRateLimiter;
  }

  public KeyResolver getDefaultKeyResolver() {
    return defaultKeyResolver;
  }


  /**
   * 拦截过滤规则
   *
   * @return org.springframework.cloud.gateway.filter.GatewayFilter
   * @Param config
   * @author peikunkun
   * @date 2021/1/29 0029 下午 1:52
   * @since
   */
  @Override
  public GatewayFilter apply(Config config) {

    return null;
  }

  /**
   * 网关的配置类
   *
   * @author peikunkun
   * @Param null
   * @return
   * @date 2021/1/29 0029 下午 1:48
   * @since
   */
  @Data
  public static class Config {

    /**
     * 值解析器
     */
    private KeyResolver keyResolver;

    /**
     * 限制规则-这个到时候自己去实现
     */
    private RateLimiter rateLimiter;

    /**
     * 状态码
     */
    private HttpStatus statusCode = HttpStatus.TOO_MANY_REQUESTS;

  }


}
